//
//  ViewController.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/14.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "ViewController.h"
#import "YWCell.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "YWHttpTool.h"
#import <UIView+SDAutoLayout.h>
#import "DataModels.h"
#import "YWHeadView.h"
#import "YWAddViewController.h"
#import "YWWebViewController.h"
#import "YWLiveController.h"


static NSString *const CellReuseIdentifier = @"CellReuseIdentifier_YWcell";




@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSArray *topDataArray;
@end

@implementation ViewController
{
    NSArray *dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpViews];
    [self requestData];
    [self setUpAddController];
}


-(void)setUpAddController
{
    
}



-(void)setUpViews
{
    [self.view addSubview:self.tableView];
    UIButton *liveBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 67/1.5, 43/1.5)];
    [liveBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [liveBtn setBackgroundImage:[UIImage imageNamed:@"night_nav_live_room_image"] forState:UIControlStateNormal];
    [liveBtn addTarget:self action:@selector(liveAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithCustomView:liveBtn];
    self.navigationItem.leftBarButtonItem = left;
}

-(void)requestData
{
    [YWHttpTool GET:URLPATH params:nil success:^(NSDictionary *responseObject) {
        
        YWDataNSObject *dataObject    = [YWDataNSObject modelObjectWithDictionary:responseObject];
        dataArray                     = dataObject.t1348647909107;
        YWDataT1348647909107 *topData = [dataArray firstObject];
        self.topDataArray             = topData.ads;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}


#pragma mark - 懒加载 -
-(UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView            = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MAINWIDTH, MAINHEIGHT)];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        [_tableView registerClass:[YWCell class] forCellReuseIdentifier:CellReuseIdentifier];
    }
    return _tableView;
}

-(NSArray *)topDataArray
{
    if (!_topDataArray)
    {
        _topDataArray = [NSArray array];
    }
    return _topDataArray;
}

#pragma mark - delegate     and    dataSource -


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YWHeadView *headView = [YWHeadView new];
    headView.ModelAds    = self.topDataArray;
    return headView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [YWHeadView headHeight];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count-1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:MAINWIDTH tableView:tableView];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWCell *cell   = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier];

    cell.modelData = dataArray[indexPath.row+1];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWDataT1348647909107 *mod = dataArray[indexPath.row+1];
    YWWebViewController *webViewVC = [YWWebViewController new];
    webViewVC.url = mod.replyid;
    webViewVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:webViewVC animated:YES];
}

-(void)liveAction
{
    YWLiveController *liveVC    = [YWLiveController new];
    liveVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:liveVC animated:YES];
}

@end
